# -*- encoding=UTF-8 -*-
import json
from datetime import datetime
from time import sleep

import requests
import uiautomator2 as u1


class upgrade:

    def __init__(self, devices_name, packageName):
        self.packageName = packageName
        self.d = u1.connect(devices_name)
        self.d.unlock()
        self.d.press("home")

    def set_up(self):
        self.d.app_stop(self.packageName)
        sleep(1)
        self.d.app_start(self.packageName)
        gotimes = 0
        while True:
            if self.d(text="首页").exists:
                sleep(3)
                break
            else:
                sleep(3)
                gotimes += 1
            if gotimes == 4:
                break

    def test_down(self):
        for quit_n in range(2):
            sleep(1)
            if self.d(text="首页").exists:
                break
            else:
                self.d.press("back")
        self.d.app_stop(self.packageName)

    def start_upgrade(self, test_num):
        for i in range(5):
            self.d(resourceId="com.heytap.smarthome:id/iv_company").click(timeout=5)
            sleep(3)
            if self.d(resourceId="com.heytap.camera.gwell:id/live_videoview_container").exists:
                break

        self.d.xpath('//androidx.appcompat.widget.LinearLayoutCompat').click(timeout=5)
        self.d(text="固件版本").click(timeout=5)
        self.d(text="升级").click(timeout=5)

        start_time = datetime.now()
        pass_or_not = self.d(text="升级成功").wait(timeout=200)
        end_time = datetime.now()
        total_time = end_time - start_time
        dingTalk = DingMessage()

        if pass_or_not:
            dingTalk.test_pass(take_time=total_time, test_num=test_num)
        else:
            dingTalk.test_fail(test_num=test_num)


class DingMessage:
    def __init__(self):
        self.URL = "https://oapi.dingtalk.com/robot/send?" \
                   "access_token=b27e1ae29115682e20e573e63e7f49533ee77c17aa193fc356139356fa40512e"  # Webhook地址
        self.headers = {'Content-Type': 'application/json'}

    def test_fail(self, test_num):
        data_dict = {
            "msgtype": "markdown",
            "markdown": {
                "title": "升级压力测试异常提示",
                "text": "#### **<font color=#FF0000>第" + str(test_num) + "次测试异常**</font>\n\n"
            }
        }
        json_data = json.dumps(data_dict)
        requests.post(self.URL, data=json_data, headers=self.headers, timeout=3)

    def test_pass(self, test_num, take_time):
        data_dict = {
            "msgtype": "markdown",
            "markdown": {
                "title": "升级压力测试通过提示",
                "text": "#### **<font color=#7CFCOO>第" + str(test_num)
                        + "次测试通过**</font>\n\n""> **升级消耗时长:** <font color=#000000>"
                        + str(take_time) + "</font>\n\n"
            }
        }
        json_data = json.dumps(data_dict)
        requests.post(self.URL, data=json_data, headers=self.headers, timeout=3)


if __name__ == '__main__':
    test_time = int(input("请输入测试次数："))
    ug = upgrade(devices_name="0.0.0.0", packageName='com.heytap.smarthome')
    ug.set_up()

    for i in range(test_time):
        ug.start_upgrade(test_num=i)
